package com.example.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.backend.entity.DingDan;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DingDanMapper extends BaseMapper<DingDan> {
    /**
     * 通过商家id查询当日已支付的所有订单信息
     */
    @Select("select * from wm_dd where sjid = #{sjid} and Date(ddsj) = CURDATE() and ddzt = '已支付'")
    List<DingDan> queryDingDanBySjid(int sjid);

    /**
     * 通过商家id查询当日所有未支付的订单信息
     */
    @Select("select * from wm_dd where sjid = #{sjid} and Date(ddsj) = CURDATE() and ddzt = '未支付'")
    List<DingDan> queryDingDanBySjidAndNotPay(int sjid);

    /**
     * 通过商家id查询当日的所有订单信息
     * */
    @Select("select * from wm_dd where sjid = #{sjid} and Date(ddsj) = CURDATE()")
    List<DingDan> queryDingDanBySjidAndDate(int sjid);

    /**
     * 通过商家id统计不同用户数量（已支付订单）
     */
    @Select("select count(DISTINCT yhid) from wm_dd where sjid = #{sjid} and ddzt = '已支付'")
    int countDistinctUsersBySjid(int sjid);


    /**
     * 通过用户id和订单时间查询订单信息
     */
    @Select("select * from wm_dd where yhid = #{param1} and ddsj = DATE_FORMAT(#{param2}, '%Y-%m-%d %H:%i:%s')")
    DingDan queryDingDanByYhidAndDdsj(Integer yhid, String ddsj);

    /**
     * 通过商家id查询未配送订单
     */
    @Select("select * from wm_dd where ddid not in (select ddid from wm_ps) and sjid = #{sjid} and ddzt='已支付'")
    List<DingDan> querydd(int sjid);

    /**
     * 通过商家id查询已支付的所有订单
     */
    @Select("select * from wm_dd where sjid = #{sjid} and ddzt='已支付'")
    List<DingDan> queryAllDingDanBySjid(int sjid);

    /**
     * 通过商家id查询待配送的订单
     */
    @Select("select * from wm_dd where ddid in (select ddid from wm_ps where pszt=0) and sjid = #{sjid} and ddzt='已支付' ")
    List<DingDan> queryDaiPeiSongDingDanBySjid(Integer sjid);
}
